	package browser.service;

	import browser.dao.UserInfo;
	import java.io.*;
	import java.sql.*;
	import java.util.Calendar;
	import java.util.List;
	import javax.servlet.*;
	import javax.servlet.http.*;
	import org.hibernate.Query;
	import org.hibernate.Session;
	import org.hibernate.SessionFactory;
	import org.hibernate.Transaction;
	import org.hibernate.cfg.Configuration;

	/**
	 * Servlet that verify user log in
	 *
	 */
	public class ProcessUserLogin extends HttpServlet {

		public static Connection conn;
		public static Statement stmt;
		public static String uAccess;

		public void doGet(HttpServletRequest request,
				HttpServletResponse response) throws ServletException,
				IOException {

			// doPost(request,response);
		}

		public void doPost(HttpServletRequest request,
				HttpServletResponse response) throws ServletException,
				IOException {

			Configuration cfg = new Configuration();
			SessionFactory sessions = cfg.configure().buildSessionFactory();
			Session session = sessions.openSession();
			Transaction tx = session.beginTransaction();

			response.setContentType("text/html");
			PrintWriter out = response.getWriter();

			String uid[] = request.getParameterValues("uid");
			String pwd[] = request.getParameterValues("password");

			try {
				Class.forName("oracle.jdbc.driver.OracleDriver");
			} catch (ClassNotFoundException e) {
				out.println("Error loading the Driver:" + e.getMessage());
				return;
			}

			conn = null;

			try {
				conn = DriverManager.getConnection("jdbc:oracle:thin:" + uid[0]
						+ "/" + pwd[0] + "@oraserver.cs.luc.edu:1521:cs");
			} catch (SQLException e1) {
				out.println("Error connecting to Oracle:" + e1.getMessage());
				return;
			}
			if (conn == null) {
				out.println("Null Connection");
				return;
			}

			request.getSession(true).setAttribute("expired", false);

			boolean OK = false;
			try {
				List l = session.createQuery("from UserInfo where userid=?")
						.setParameter(0, uid[0]).list();
				uAccess = String.valueOf(System.currentTimeMillis());
				String lAccess = Calendar.getInstance().getTime().toString();
				if (l.size() == 0) {
					UserInfo user = new UserInfo(uid[0], pwd[0], uAccess,
							lAccess);
					session.save(user);
					tx.commit();
					session.close();
				} else {

					String sql = "update UserInfo set upassword='" + pwd[0]
							+ "',uaccess='" + uAccess + "',laccess='" + lAccess
							+ "' where userid='" + uid[0] + "'";
					Query q = session.createQuery(sql);
					q.executeUpdate();
					tx.commit();
					session.close();

				}

				OK = true;
			} catch (Exception e) {
				out.println(e.getMessage());
				return;
			}
			;

			try {
				conn.close();
			} catch (SQLException e) {
				out.println("executeQuery " + e.getMessage());
				return;
			}

			if (OK) {
				out.println("<html>");
				out.println("  <head>");
				out.println("    <title>Welcome to Loyola Oracle</title>");
				out.println("  <link type=\"text/css\" rel=\"stylesheet\" href=\"main.css\"/></head>");
				out.println("  <frameset rows=\"20%,*\">");
				out.println("    <frame src=\"/DatabaseBrowser/Banner?uid="
						+ uid[0] + "&uAccess=" + uAccess
						+ "\" name=\"banner\">");
				out.println("  <frameset cols=\"25%,*\">");
				out.println("    <frame src=\"/DatabaseBrowser/OPMenu?uid="
						+ uid[0] + "&uAccess=" + uAccess + "\" name=\"menu\">");
				out
						.println("    <frame src=\"Display.html\" name=\"view_window\">");
				out.println("  </frameset>");
				out.println("  </frameset>");
				out.println("</html>");
			}

			out.close();

		}
	}
